package com.example.demo.Controller;

import com.example.demo.Dto.*;

import com.example.demo.Service.GoodsService;
import com.example.demo.VO.IPage;
import com.example.demo.VO.ProductInfoVo;
import com.example.demo.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/products")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    /**
     * 获取商品信息
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    public ResponseResult getgoods(@PathVariable Integer productId){
        ProductInfoVo getgoods = goodsService.getgoods(productId);
        return new  ResponseResult().ok(getgoods);
    }
    /**
     * 指定某种类别查询商品信息
     * @param ListProductsDto
     * @return
     */
    @GetMapping
    public ResponseResult categoryNamegetgoods(@RequestBody ListProductsDto ListProductsDto){
        IPage iPage = goodsService.categoryNamegetgoods(ListProductsDto);
        return new  ResponseResult().ok(iPage);
    }
    /**
     * 3. 指定条件查询商品信息
     * @param searchProductsDto
     * @return
     */
    @GetMapping("/search")
    public ResponseResult searchgetgoods(@RequestBody SearchProductsDto searchProductsDto){
        IPage iPage = goodsService.searchgetgoods(searchProductsDto);
        return new  ResponseResult().ok(iPage);
    }
    /**
     * 4. 添加库存
     * @return
     */
    @PutMapping("/add")
    public ResponseResult add(@RequestBody AddProductDto addProductDto){
        goodsService.add(addProductDto);
        return new  ResponseResult().ok(null,"库存添加成功");
    }
    /**
     * 5. 创建商品
     * @return
     */
    @PostMapping
    public ResponseResult create(@RequestBody CreateProductDto createProductDto){
        if (createProductDto.getDescription() == null || createProductDto.getDescription().equals("")){
            return new  ResponseResult().error(400,"请输入商品描述");
        }
        goodsService.create(createProductDto);
        return new  ResponseResult().ok(null,"商品创建成功");
    }
    /**
     * 6. 批量创建商品
     * @return
     */
    @PostMapping("/batch")
    public ResponseResult batch(@RequestBody List<CreateProductDto> createProductDtos){
        if (createProductDtos.size() == 0){
            return new  ResponseResult().error(400,"请输入要创建的商品信息");
        }
        goodsService.batch(createProductDtos);
        return new  ResponseResult().ok(null,"商品创建成功");
    }
    /**
     * 7. 更新商品信息
     * @return
     */
    @PutMapping
    public ResponseResult update(@RequestBody UpdateProductDto updateProductDto){
        goodsService.update(updateProductDto);
        return new  ResponseResult().ok(null,"商品删除成功");
    }
}
